<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
 * 后台菜单管理
 * @author 		jaypha
 * @qq			168007694
 * @email		psufool@163.com
 * @home page	www.psufool.cn
 */
class Article extends Admin_Controller{
	
	function __construct(){
		parent::__construct();
		$this->load->model('JayphaDB');
		$this->load->model('JayphaTemplate');
		$this->adminCurrentTemplate = $this->JayphaDB->getConfigValue('adminCurrentTemplate');
		if(is_null($this->adminCurrentTemplate) or empty($this->adminCurrentTemplate)){
			$this->adminCurrentTemplate = 'default';
		}
		$this->dbTablePrefix = $this->config->item('db_table_prefix');
	}
	
	function index(){
		parse_str($this->input->server('QUERY_STRING'),$this->input->get());		
		if(!$this->noLoginErr()) return false;
		$data['adminCurrentTemplate'] = $this->adminCurrentTemplate;
		$tvName = "archives";
		$channel_tv = "channel";
		$admin_main_con = $this->uri->segment(1).'/'.$this->uri->segment(2);
		$subjoin_condition['db_where'] = array('upper(adminMainCon)'=>strtoupper($admin_main_con));
		$subjoin_condition['db_or_where'] = array('upper(adminMainCon)'=>strtoupper($admin_main_con.'/index'));
		$subjoin = $this->JayphaDB->getDB(0,1,'',$this->dbTablePrefix.$channel_tv,$subjoin_condition,'',true);
		if(count($subjoin)>0){
			$channel_id = $subjoin[0]->id;
			$channel_title = $subjoin[0]->title;
			$channel_subjoinTable = $subjoin[0]->subjoinTable;
			$channel_edit_con = $subjoin[0]->adminEditCon;
			$channel_identifier = $subjoin[0]->identifier;
		}else{
			exit;
		}
		if($this->input->server('REQUEST_METHOD')=='POST'){
			$active = $this->input->post('active');
			$del = $this->input->post('del');
			$id = $this->input->post('id');
			if(is_array($id)){
				$id = implode(',',$id);
			}
			$data['disabled'] = $this->input->post('disabled');
			$data['ismake'] = $this->input->post('ismake');
			$data['categoryId'] = $this->input->post('categoryId');
			$data['title'] = $this->input->post('title');
			$data['keywords'] = $this->input->post('keywords');
			$data['upTimeStart'] = $this->input->post('upTimeStart');
			$data['upTimeEnd'] = $this->input->post('upTimeEnd');
			$data['createTimeStart'] = $this->input->post('createTimeStart');
			$data['createTimeEnd'] = $this->input->post('createTimeEnd');
		}else{
			$data['disabled'] = $this->input->get('disabled');
			$data['ismake'] = $this->input->get('ismake');
			$data['categoryId'] = $this->input->get('categoryId');
			$data['title'] = $this->input->get('title');
			$data['keywords'] = $this->input->get('keywords');
			$data['upTimeStart'] = $this->input->get('upTimeStart');
			$data['upTimeEnd'] = $this->input->get('upTimeEnd');
			$data['createTimeStart'] = $this->input->get('createTimeStart');
			$data['createTimeEnd'] = $this->input->get('createTimeEnd');
		}
		$data['title'] = urldecode($data['title']);
		$data['keywords'] = urldecode($data['keywords']);
		$condition = array('channelId'=>$channel_id);
		$db_like_key = array();
		$db_like_value = array();
		if(strlen($data['disabled'])>0){
			$condition['disabled'] = $data['disabled'];
		}		
		if(strlen($data['ismake'])>0){
			$condition['ismake'] = $data['ismake'];
		}
		if(strlen($data['categoryId'])>0){
			$condition['categoryId'] = $data['categoryId'];
		}
		if(strlen($data['title'])>0){
			array_push($db_like_key,'upper(title)');
			array_push($db_like_value,strtoupper($data['title']));
		}
		if(strlen($data['upTimeStart'])>0){
			$condition['updateTime >= '] = strtotime($data['upTimeStart']);
		}
		if(strlen($data['upTimeEnd'])>0){
			$condition['updateTime <= '] = strtotime($data['upTimeEnd']) + 24*60*60;
		}
		if(strlen($data['createTimeStart'])>0){
			$condition['createtime >= '] = strtotime($data['createTimeStart']);
		}
		if(strlen($data['createTimeEnd'])>0){
			$condition['createtime <= '] = strtotime($data['createTimeEnd']) + 24*60*60;
		}
		if(count($db_like_key)>0){
			$condition['db_like'] = array_combine($db_like_key,$db_like_value);
		}
		unset($db_like_key);
		unset($db_like_value);
		$where = $condition;
		if($this->input->server('REQUEST_METHOD')=='POST'){
			switch (strtoupper($active)){
				case 'DEL':
					$del_condition = $del_subjointable_condition = array();
					if(strlen($del)==0){
						$condition['db_where_in'] = array('id',$id);
					}
					foreach($condition as $k=>$v){
						if(strtolower($k)==='channelid' || strtolower($k)==='categoryid' || strtolower($k)==='db_where_in'){
							$del_condition[$k] = $v;
						}
					}
					$del_subjointable_condition = $del_condition;
					if(array_key_exists('db_where_in',$del_subjointable_condition)){
						$del_subjointable_condition['db_where_in'][0]='aid';
					}			
					$operationData = array(
						'adminId'=>$this->session->userdata('adminUserId'),
						'title'=>'删除文档',
						'url'=>$this->input->server('REQUEST_URI'),
						'remark'=>'删除文档，删除内容：'.json_encode($this->JayphaDB->getSingleDB($this->dbTablePrefix.$tvName,$del_condition))
					);
					$this->db->trans_start();
					$this->JayphaDB->setAdminOperation($operationData);
					$this->JayphaDB->delDB($this->dbTablePrefix.$channel_subjoinTable,$del_subjointable_condition);
					$this->JayphaDB->delDB($this->dbTablePrefix.$tvName,$del_condition);
					$this->db->trans_complete();
					unset($del_condition);
					unset($del_subjointable_condition);
					break;
				case 'SETPARAM':
					//批量设置属性
					$data['click'] = $this->input->post('click');
					$data['flag'] = $this->input->post('flag');
					$updata = array();
					if(strlen($data['disabled'])>0){
						$updata['disabled'] = $data['disabled'];
					}
					if(strlen($data['ismake'])>0){
						$updata['ismake'] = $data['ismake'];
					}
					if(is_array($data['flag'])){
						$flag = '';
						foreach($data['flag'] as $flag_item){
							if($flag=='')
								$flag = $flag_item;
							else
								$flag .= ','.$flag_item;
						}
						$updata['flag'] = $flag;
					}else{
						$updata['flag'] = null;
					}
					if(strlen($data['click'])>0){
						if(is_numeric($data['click'])){
							$updata['click'] = (int)$data['click'];
						}
					}
					$condition2['db_where_in'] = array('id',$id);
					$this->JayphaDB->updateDB($updata,$this->dbTablePrefix.$tvName,$condition2);
					break;
				case 'DISABLED':
					//启用状态
					$condition2['db_where_in'] = array('id',$id);
					$disabled = $this->input->post('disabled');
					$this->JayphaDB->updateDB(array('disabled'=>$disabled),$this->dbTablePrefix.$tvName,$condition2);
					break;
				case 'REMOVE':
					//移动
					$categoryId = $this->input->post('categoryId');
					$condition2['db_where_in'] = array('id',$id);
					$condition3['db_where_in'] = array('aid',$id);
					$moveData = array('categoryId'=>$categoryId);
					$this->db->trans_start();					
					$channelDB = $this->dbTablePrefix.$tvName;
					$channelList = $this->JayphaDB->getDB(0,0,'channelId',$channelDB,$condition2,'',true);
					foreach($channelList as $item){
						$moveData['channelId'] = $item->channelId;
						$subjoinTable = $this->JayphaDB->getDBValue('subjoinTable',$this->dbTablePrefix.'channel',array('id'=>$item->channelId),'');
						$this->JayphaDB->updateDB($moveData,$this->dbTablePrefix.$subjoinTable,$condition3);
					}					
					$this->JayphaDB->updateDB($moveData,$this->dbTablePrefix.$tvName,$condition2);
					$this->db->trans_complete();
					break;
			}
		}
		$config['base_url'] = site_url("$admin_main_con/index/");
		$config['total_rows'] = $this->JayphaDB->getDBCount($this->dbTablePrefix.$tvName,$where);
		$config['per_page'] = $this->JayphaDB->getConfigValue('adminPageSize');
		$config['uri_segment'] = 4;
		$config['num_links'] = 5;
		$config['first_link'] = '|<';
		$config['last_link'] = '>|';
		$config['next_link'] = '>>';
		$config['prev_link'] = '<<';
		$this->pagination->initialize($config);
		$data['currentSite'] = '内容管理<span class="split">>></span>'.$channel_title.'管理<span class="split">>></span>文档列表';
		$data['phpSelf'] = $config['base_url'];
		$data['pageTag'] = $channel_title.'管理';
		$data['editPage'] = site_url($channel_edit_con);
		$data['editTitle'] = '修改文档';
		$data['addTitle'] = '添加文档';
		$data['perm'] = $channel_identifier;
		$data['adminPermission'] = $this->JayphaDB->getAdminPermission($this->session->userdata('adminUserId'));
		$data['tvName'] = $tvName;
		$order = 'id';
		$num = $this->uri->segment(4,0);
		$data['archivesList'] = $this->JayphaDB->getDB($config['per_page'],$num,'',$this->dbTablePrefix.$tvName,$where,$order,false);
		foreach($data['archivesList'] as $archives_item){
			if($archives_item->categoryId==0)
				$archives_item->categoryTitle = '未分配';
			else{
				$archives_item->categoryTitle = $this->JayphaDB->getDBValue('title',$this->dbTablePrefix.'category',array('id'=>$archives_item->categoryId),'');
			}
		}
		$categoryAll = $this->JayphaDB->objToArr($this->JayphaDB->getDB(0,0,'id,parentId,title',$this->dbTablePrefix.'category',array('channelId'=>$channel_id),'parentId,id',false));
		$data['categoryList'] = $this->JayphaDB->getChildrenList($categoryAll,0,1);
		$this->load->view("admin/$this->adminCurrentTemplate/article",$data);
	}
	
	function edit(){
		parse_str($this->input->server('QUERY_STRING'),$this->input->get());		
		if(!$this->noLoginErr()) return false;
		$data['adminCurrentTemplate'] = $this->adminCurrentTemplate;
		$tvName = "archives";
		$data['tvName'] = $tvName;
		$channel_tv = "channel";
		$admin_main_con = $this->uri->segment(1).'/'.$this->uri->segment(2);
		$subjoin_condition['db_where'] = array('upper(adminEditCon)'=>strtoupper($admin_main_con));
		$subjoin_condition['db_or_where'] = array('upper(adminEditCon)'=>strtoupper($admin_main_con.'/edit'));
		$subjoin = $this->JayphaDB->getDB(0,1,'',$this->dbTablePrefix.$channel_tv,$subjoin_condition,'',true);
		if(count($subjoin)>0){
			$channel_id = $subjoin[0]->id;
			$channel_title = $subjoin[0]->title;
			$channel_subjoinTable = $subjoin[0]->subjoinTable;
			$channel_edit_con = $subjoin[0]->adminEditCon;
			$channel_identifier = $subjoin[0]->identifier;
			$channel_fieldset = $subjoin[0]->fieldset;
			$subjoin_fieldset = $this->JayphaTemplate->getSubjoinFields($channel_fieldset,3);
		}else{
			exit;
		}
		$categoryAll = $this->JayphaDB->objToArr($this->JayphaDB->getDB(0,0,'id,parentId,title',$this->dbTablePrefix.'category',array('channelId'=>$channel_id),'parentId,id',false));
		$data['categoryList'] = $this->JayphaDB->getChildrenList($categoryAll,0,1);
		$data['perm'] = $channel_identifier;
		$this->CKEditor = new CKEditor();
		$this->CKEditor->basePath = base_url().$this->config->item('CKEditor_path');
		$this->CKEditor->config['height'] = 126;
		$this->CKEditor->config['toolbar'] = 'full';
		$this->CKEditor->config['language'] = 'zh-cn';
		$this->CKEditor->config['toolbarStartupExpanded'] = true;
		$this->CKEditor->returnOutput = true;
		CKFinder::SetupCKEditor($this->CKEditor,$this->config->item('CKFinder_path'));
		
		if($this->input->server('REQUEST_METHOD')=='POST'){
			$id = $this->input->post('id');
			$disabled = $this->input->post('disabled');
			$ismake = $this->input->post('ismake');
			$flag = $this->input->post('flag');
			$flag_string = '';
			if(is_array($flag)){
				if(count($flag)>0){
					foreach($flag as $flag_item){
						if(strlen($flag_string)==0){
							$flag_string = $flag_item;
						}else{
							$flag_string .= ','.$flag_item;
						}
					}
				}
			}
			if(strlen($flag_string)>0){
				$flag = $flag_string;				
			}else{
				$flag = null;
			}
			unset($flag_string);
			$redirecturl = $this->input->post('redirecturl');
			$source = $this->input->post('source');
			$author = $this->input->post('author');
			$click = $this->input->post('click');
			$color = $this->input->post('color');
			$goodpost = $this->input->post('goodpost');
			$badpost = $this->input->post('badpost');
			$filename = $this->input->post('filename');
			$template = $this->input->post('template');
			$updateTime = strtotime($this->input->post('updateTime'));
			$createTime = strtotime($this->input->post('createTime'));
			$categoryId = $this->input->post('categoryId');
			$oIndex = $this->input->post('oIndex');
			$title = $this->input->post('title');
			$litpic = $this->input->post('litpic');
			$shortTitle = $this->input->post('shortTitle');
			$keywords = $this->input->post('keywords');
			$description = $this->input->post('description');
			foreach($subjoin_fieldset as $subjoin_field_key=>$subjoin_field_value){
				$$subjoin_field_key = $this->input->post($subjoin_field_key);
			}		
			if(strlen($id)>0){
				if($this->JayphaDB->getDBCount($this->dbTablePrefix.$tvName,array('id != '=>$id,'categoryId'=>$categoryId,'upper(title)'=>strtoupper($title)))>0){
					unset($data['errorScript']['lhgdialog_alert_mess']);
					$data['errorScript'] = array('lhgdialog_alert_mess'=>array(
							'width'=>300,
							'height'=>125,
							'title'=>'修改失败',
							'content'=>'<span style="line-height:20px; display:block; padding:0 3px; padding-top:5px;">对不起，此文档标题已经存在，请选用其它标题！</span>',
							'url'=>'history.back()',
							'target'=>'_self',
							'conver'=>true,
							'resize'=>false,
							'drag'=>false,
							'btns'=>true,
							'btnText'=>'返回',
							'rightClose'=>false
					));
					$this->load->view("admin/$this->adminCurrentTemplate/error",$data);
					return false;
				}
				$where = array('id'=>$id);
				$subjoin_where = array('aid'=>$id);
				$updateData = array(
						'disabled'=>$disabled,
						'ismake'=>$ismake,
						'flag'=>$flag,
						'redirecturl'=>$redirecturl,
						'source'=>$source,
						'author'=>$author,
						'click'=>$click,
						'color'=>$color,
						'goodpost'=>$goodpost,
						'badpost'=>$badpost,
						'filename'=>$filename,
						'template'=>$template,
						'updateTime'=>$updateTime,
						'createTime'=>$createTime,
						'categoryId'=>$categoryId,
						'oIndex'=>$oIndex,
						'title'=>$title,
						'litpic'=>$litpic,
						'shortTitle'=>$shortTitle,
						'keywords'=>$keywords,
						'description'=>$description
				);
				$subjoin_updateData = array('categoryid'=>$categoryId);
				foreach($subjoin_fieldset as $subjoin_field_key=>$subjoin_field_value){
					$subjoin_updateData[$subjoin_field_key] = $$subjoin_field_key;
					if(strtolower($subjoin_field_value['type'])=='datetime'){
							$subjoin_updateData[$subjoin_field_key] = strtotime($subjoin_updateData[$subjoin_field_key]);
					}
				}
				$this->db->trans_start();
				$this->JayphaDB->updateDB($updateData,$this->dbTablePrefix.$tvName,$where);
				$this->JayphaDB->updateDB($subjoin_updateData,$this->dbTablePrefix.$channel_subjoinTable,$subjoin_where);
				$this->db->trans_complete();
				unset($data['errorScript']['lhgdialog_alert_mess']);
				$data['errorScript'] = array('lhgdialog_alert_mess'=>array(
						'width'=>220,
						'height'=>108,
						'title'=>'修改成功 ',
						'content'=>'您的文档已成功修改！',
						'url'=>'location.reload',
						'target'=>'_self',
						'conver'=>true,
						'resize'=>false,
						'drag'=>false,
						'btns'=>true,
						'btnText'=>'关闭',
						'rightClose'=>false
				));
				$this->load->view("admin/$this->adminCurrentTemplate/error",$data);
				return false;
			}else{
				$addData = array(
						'disabled'=>$disabled,
						'ismake'=>$ismake,
						'flag'=>$flag,
						'redirecturl'=>$redirecturl,
						'source'=>$source,
						'author'=>$author,
						'click'=>$click,
						'color'=>$color,
						'goodpost'=>$goodpost,
						'badpost'=>$badpost,
						'filename'=>$filename,
						'template'=>$template,
						'updateTime'=>$updateTime,
						'createTime'=>$createTime,
						'categoryId'=>$categoryId,
						'oIndex'=>$oIndex,
						'title'=>$title,
						'litpic'=>$litpic,
						'shortTitle'=>$shortTitle,
						'keywords'=>$keywords,
						'description'=>$description
				);
				$subjoin_addData = array('channelid'=>$channel_id,'categoryid'=>$categoryId);
				foreach($subjoin_fieldset as $subjoin_field_key=>$subjoin_field_value){
					$subjoin_addData[$subjoin_field_key] = $$subjoin_field_key;
					if(strtolower($subjoin_field_value['type'])=='datetime'){
							$subjoin_addData[$subjoin_field_key] = strtotime($subjoin_addData[$subjoin_field_key]);
					}
				}
				if($this->JayphaDB->getDBCount($this->dbTablePrefix.$tvName,array('categoryId'=>$categoryId,'upper(title)'=>strtoupper($title)))>0){
					unset($data['errorScript']['lhgdialog_alert_mess']);
					$data['errorScript'] = array('lhgdialog_alert_mess'=>array(
							'width'=>300,
							'height'=>125,
							'title'=>'添加失败',
							'content'=>'<span style="line-height:20px; display:block; padding:0 3px; padding-top:5px;">对不起，您预添加的文档已经存在，请取其它文档标题！</span>',
							'url'=>'history.back();',
							'target'=>'_self',
							'conver'=>true,
							'resize'=>false,
							'drag'=>false,
							'btns'=>true,
							'btnText'=>'返回',
							'rightClose'=>false
					));
					$this->load->view("admin/$this->adminCurrentTemplate/error",$data);
					return false;
				}
				$this->db->trans_start();
				$this->JayphaDB->insertDB($this->dbTablePrefix.$tvName,$addData,true);
				$subjoin_addData['aid'] = $this->db->insert_id();
				$this->JayphaDB->insertDB($this->dbTablePrefix.$channel_subjoinTable,$subjoin_addData,true);
				$this->db->trans_complete();
				unset($data['errorScript']['lhgdialog_alert_mess']);
				$data['errorScript'] = array('lhgdialog_alert_mess'=>array(
						'width'=>180,
						'height'=>108,
						'title'=>'添加成功 ',
						'content'=>'您的文档已成功添加！',
						'url'=>'location.reload',
						'target'=>'_self',
						'conver'=>true,
						'resize'=>false,
						'drag'=>false,
						'btns'=>true,
						'btnText'=>'关闭',
						'rightClose'=>false
				));
				$this->load->view("admin/$this->adminCurrentTemplate/error",$data);
				return false;
			}
		}else{
			$data['id'] = $this->uri->segment(4);
			if(strlen($data['id'])>0){
				$field_list = $this->dbTablePrefix.$tvName.'.*';
				foreach($subjoin_fieldset as $subjoin_field_key=>$subjoin_field_value){
					$field_list .= ','.$this->dbTablePrefix.$channel_subjoinTable.'.'.$subjoin_field_key;
				}
				$table_name = array('tableName'=>$this->dbTablePrefix.$tvName,
									'db_join'=>array(
												array('tableName'=>$this->dbTablePrefix.$channel_subjoinTable,'conditions'=>$this->dbTablePrefix.$channel_subjoinTable.'.aid='.$this->dbTablePrefix.$tvName.'.id')
													)
									);
				
				foreach($this->JayphaDB->getDB('','',$field_list,$table_name,array($this->dbTablePrefix.$tvName.'.id'=>$data['id']),'',false) as $item){					
					$data['disabled'] = $item->disabled;
					$data['ismake'] = $item->ismake;
					$data['flag'] = $item->flag;
					$data['redirecturl'] = $item->redirecturl;
					$data['source'] = $item->source;
					$data['author'] = $item->author;
					$data['click'] = $item->click;
					$data['color'] = $item->color;
					$data['goodpost'] = $item->goodpost;
					$data['badpost'] = $item->badpost;
					$data['filename'] = $item->filename;
					$data['template'] = $item->template;
					$data['updateTime'] = date('Y-m-d H:m:s',$item->updateTime);
					$data['createTime'] = date('Y-m-d H:m:s',$item->createTime);
					$data['categoryId'] = $item->categoryId;
					$data['oIndex'] = $item->oIndex;
					$data['title'] = $item->title;
					$data['litpic'] = $item->litpic;
					$data['shortTitle'] = $item->shortTitle;
					$data['keywords'] = $item->keywords;
					$data['description'] = $item->description;
					foreach($subjoin_fieldset as $subjoin_field_key=>$subjoin_field_value){
						$data[$subjoin_field_key] = $item->$subjoin_field_key;
						if(strtolower($subjoin_field_value['type'])=='htmltext' || strtolower($subjoin_field_value['type'])=='textdata'){
							$data[$subjoin_field_key] = $this->CKEditor->editor($subjoin_field_key,$data[$subjoin_field_key]);
						}else if(strtolower($subjoin_field_value['type'])=='datetime'){
							$data[$subjoin_field_key] = date('Y-m-d H:m:s',$data[$subjoin_field_key]);
						}
					}
					$data['fieldset'] = $subjoin_fieldset;
				}
			}else{
				$data['disabled'] = 1;
				$data['ismake'] = 0;
				$data['flag'] = '';
				$data['redirecturl'] = '';
				$data['click'] = 100;
				$data['color'] = '';
				$data['author'] = '';
				$data['source'] = '';
				$data['goodpost'] = 0;
				$data['badpost'] = 0;				
				$data['filename'] = '';
				$data['template'] = '';
				$data['updateTime'] = $data['createTime'] = date('Y-m-d H:m:s',now());				
				$data['categoryId'] = 0;
				$data['oIndex'] = 50;
				$data['title'] = '';
				$data['litpic'] = '';
				$data['shortTitle'] = '';
				$data['keywords'] = '';
				$data['description'] = '';
				foreach($subjoin_fieldset as $subjoin_field_key=>$subjoin_field_value){
					$data[$subjoin_field_key] = '';
					if(strtolower($subjoin_field_value['type'])=='htmltext' || strtolower($subjoin_field_value['type'])=='textdata'){
						$data[$subjoin_field_key] = $this->CKEditor->editor($subjoin_field_key,$data[$subjoin_field_key]);
					}else if(strtolower($subjoin_field_value['type']=='datetime')){
						$data[$subjoin_field_key] = date('Y-m-d H:m:s',now());
					}
				}
				$data['fieldset'] = $subjoin_fieldset;
			}		
			
			$this->load->view("admin/$this->adminCurrentTemplate/articleEdit",$data);
		}
	}
}

/* End of file article.php */
/* Location: ./application/controllers/admin/article.php */